<template>
    <div id="seachDialog">
      <a-modal
        title="添加团队成员"
        v-model="teamData.show"
        width="450px"
        style="overflow:hidden"
        @cancel="handleClose">
        <el-row style="padding:30px">
          <el-col :span="24">
            <a-tree-select
              showSearch
              allowClear
              style="width: 100%"
              :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
              :treeData="gData"
              treeNodeFilterProp="title"
              placeholder="请选择"
              treeDefaultExpandAll
              treeCheckable
              @change="onChange"
              :value="value"
            >
            </a-tree-select>
          </el-col>
        </el-row>
        <span slot="footer" class="dialog-footer">
            <el-button size="small" type="primary" @click="handleSave">保存</el-button>
            <el-button size="small" @click="handleClose">取消</el-button>
        </span>
       

      </a-modal>
    </div>
</template>

<script>
const gData = [];
export default {
    props: ['teamData'],
    data() {
        return {
          value: undefined,
          gData,
          parames: {
              name: '',
              youxian: '',
              date: ''
          },
          userData: []
        };
    },
    mounted() {
      
    },
    methods: {
      handleClose () {
        this.teamData.show = false
      },
      getTree() { //获取组织架构树
        this.$post(this.$api.postProviderOrganizationOrganizationUserTree).then(response => {
          if (response.data.succeed) {
            this.gData = []
            if (response.data.data.length > 0) {
              response.data.data.forEach(el=>{
                this.getData(el)
                if (el.title) {
                  this.gData.push(el)
                }
              })
            }
          }
        })
      },
      getData(data) { //处理树数据
        data.title = data.label
        data.value = data.id
        data.key = data.id //
        data.disabled = data.type
        data.scopedSlots = { title: 'title', key: 'key', parentId: 'parentId' }
        if (data.children && data.children.length > 0) {
          for(var i = 0; i < data.children.length; i++) {
            this.getData(data.children[i])
          }
        }
      },
      handleSave () { //保存
        this.$emit('selectUser',this.userData)
        this.handleClose()
      },
      onChange (value,label, extra) {
        let userData = []
        for (var i = 0; i<label.length; i++) {
          userData.push({
            userId: value[i],
            userName: label[i]
          })
        }
        this.userData = userData
        this.value = value
        // this.currentValue = value
      },
    },
    computed: {
      changeDialog () {
        return this.teamData.show
      }
    }, 
    watch: {
      changeDialog: {
        handler (curVal, oldVal) {
          if (curVal) {
            this.getTree()
          } else {
            this.value = []
          }
        }
      }
    },
};
</script>
